package com.stdroom.hellomyfriend;

import com.stdroom.hellomyfriend.ui.FragmentContacts;
import com.stdroom.hellomyfriend.ui.FragmentMessageList;
import com.stdroom.hellomyfriend.ui.FragmentSendEvents;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends FragmentActivity{

	private ViewPager viewPager ;
	RadioGroup rBtnGroups = null;
	RadioButton eventsRBtn = null;		//事件按钮
	RadioButton resourceRBtn = null;	//信息资源按钮
	RadioButton contactsRBtn = null;	//联系人按钮
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		eventsRBtn = (RadioButton)findViewById(R.id.sendEvent_RBtn);
		resourceRBtn = (RadioButton)findViewById(R.id.messageResource_RBtn);
		contactsRBtn = (RadioButton)findViewById(R.id.contactor_RBtn);
		viewPager = (ViewPager)findViewById(R.id.pager);
		rBtnGroups = (RadioGroup)findViewById(R.id.rBtnGroup);
		viewPager.setAdapter(mPagerAdapter);
		rBtnGroups.setOnCheckedChangeListener(mListener);
	}

	private FragmentPagerAdapter mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			Fragment fragmentView = null;
			switch(position){
			case 0:
				eventsRBtn.setSelected(true);
				eventsRBtn.setChecked(false);
				resourceRBtn.setChecked(false);
				contactsRBtn.setChecked(false);
				resourceRBtn.setSelected(false);
				contactsRBtn.setSelected(false);
				fragmentView = new FragmentSendEvents();
				break;
			case 1:
				eventsRBtn.setChecked(false);
				eventsRBtn.setSelected(false);
				resourceRBtn.setSelected(true);
				resourceRBtn.setChecked(false);
				contactsRBtn.setChecked(false);
				contactsRBtn.setSelected(false);
				fragmentView = new FragmentMessageList();
				break;
			case 2:
				eventsRBtn.setChecked(false);
				eventsRBtn.setSelected(false);
				resourceRBtn.setChecked(false);
				resourceRBtn.setSelected(false);
				contactsRBtn.setSelected(true);
				contactsRBtn.setChecked(false);
				default: fragmentView = new FragmentContacts();
			}
			return fragmentView;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 3;
		}
		
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private RadioGroup.OnCheckedChangeListener mListener = new RadioGroup.OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if(checkedId==R.id.sendEvent_RBtn){
				viewPager.setCurrentItem(0);
			}else if(checkedId == R.id.messageResource_RBtn){
				viewPager.setCurrentItem(1);
			}else if(checkedId == R.id.contactor_RBtn){
				viewPager.setCurrentItem(2);
			}else{
				viewPager.setCurrentItem(0);
			}
			
		}
	};
}
